package com.woparker.kiosk.utils;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.woparker.kiosk.R;
import com.woparker.kiosk.adapter.CommonAdapter;
import com.woparker.kiosk.adapter.ViewHolder;
import com.woparker.kiosk.bean.SlotDTO;
import com.woparker.kiosk.view.MyDialog;

import java.util.ArrayList;
import java.util.List;


/**
 * @author Yellen.yuan
 * @ClassName: DialogUtil
 * @date 2016-11-9
 */
public class DialogUtil {

    private static DialogUtil dialogUtil = null;


    private DialogUtil() {

    }

    public static DialogUtil getInstance() {
        if (null == dialogUtil) {
            dialogUtil = new DialogUtil();
        }
        return dialogUtil;

    }

    public MyDialog dialog;

    public void showDialog(Context context, View contentView,
                           boolean isCancelable, LayoutParams params) {
        dialog = new MyDialog(context, R.style.dialog);
        dialog.createDialog(context, contentView, isCancelable, params);
    }

    public void dismiss() {
        if (null != dialog) {
            dialog.dismiss();
        }
    }

    public boolean isShowingDialog() {
        if (null == dialog || !dialog.isShowing()) {
            return false;
        }
        return true;
    }

    /**
     * 显示图片对话框
     *
     * @param bean
     */
    public static void showDialogPictrue(Context context, SlotDTO bean) {
        int[] size = CommonUtils.getScreenSize();
        int width = size[0];
        int height = size[1];
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (width*0.65), (int) (height*0.75));
        View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_showphoto, null);
        ImageView imageView = dialogView.findViewById(R.id.img);
        TextView photo_layer = dialogView.findViewById(R.id.photo_layer);
        TextView plate_start_time = dialogView.findViewById(R.id.plate_start_time);

        StringBuilder locationStr = new StringBuilder();
        if(!TextUtils.isEmpty(bean.getPlaceName())){
            locationStr.append(bean.getPlaceName());
            locationStr.append("层");

        }
        if(!TextUtils.isEmpty(bean.getArea())){
            locationStr.append(" ");
            locationStr.append(bean.getArea());
            locationStr.append("区");

        }
        if(!TextUtils.isEmpty(bean.getName())){
            locationStr.append(" ");
            locationStr.append(bean.getName());
            locationStr.append("车位");

        }
        photo_layer.setText(locationStr.toString());
        plate_start_time.setText(bean.getPlate());
        GlideUtilsKt.loadImageByUrl(imageView, bean.getPic());

        dialogView.findViewById(R.id.close_prompt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
            }
        });
        DialogUtil.getInstance().dismiss();
        DialogUtil.getInstance().showDialog(context, dialogView, true, params);

    }


    /**
     * 警告对话框
     *
     * @param context
     */
    public static void showDialogWarning(Context context) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_warning, null);
        DialogUtil.getInstance().showDialog(context, dialogView, true, params);
    }


    /**
     * 普通的确认取消对话框
     *
     * @param context
     */
    public static void showDialogSure(Context context, String title, String tips, final OnDialogItemClickListener onDialogItemClickListener) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        View dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_sure, null);
        TextView titleTv = (TextView) dialogView.findViewById(R.id.dialog_title);
        TextView tipsTv = (TextView) dialogView.findViewById(R.id.dialog_tips);
        titleTv.setText(title);
        tipsTv.setText(tips);
        dialogView.findViewById(R.id.dialog_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
                onDialogItemClickListener.onCancle();
            }
        });
        dialogView.findViewById(R.id.dialog_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
                onDialogItemClickListener.onSure();
            }
        });

        DialogUtil.getInstance().showDialog(context, dialogView, true, params);
    }


    /**
     * 车牌选择对话框
     *
     * @param context
     */
    public static void showDialogPlate(Context context, final TextView cityTv) {
        CommonAdapter commonAdapter = null;
        final List<String> itemList = new ArrayList<String>();
        initCity(itemList);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        View dialogView = LayoutInflater.from(context).inflate(R.layout.view_dialog_plate, null);
        GridView dialog_plate_choice = (GridView) dialogView.findViewById(R.id.dialog_plate_choice);
        TextView tv = (TextView) dialogView.findViewById(R.id.platetv);

        dialogView.findViewById(R.id.close_prompt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
            }
        });

        dialog_plate_choice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DialogUtil.getInstance().dismiss();
                cityTv.setText(itemList.get(position));
            }
        });

        if (null == commonAdapter) {
            commonAdapter = new CommonAdapter<String>(context,
                    itemList, R.layout.item_dialog_plate, false) {
                @SuppressWarnings("incomplete-switch")
                @Override
                public void convert(ViewHolder viewHolder, String item) {
                    // TODO Auto-generated method stub
                    viewHolder.setText(R.id.platetv, item);
                }
            };
            dialog_plate_choice.setAdapter(commonAdapter);
        } else {
            commonAdapter.notifyDataSetChanged();
        }
        DialogUtil.getInstance().showDialog(context, dialogView, true, params);
    }


    public static void initCity(List<String> itemList) {
        itemList.add("京");
        itemList.add("粤");
        itemList.add("津");
        itemList.add("冀");
        itemList.add("晋");
        itemList.add("蒙");
        itemList.add("辽");
        itemList.add("吉");
        itemList.add("黑");
        itemList.add("沪");
        itemList.add("苏");
        itemList.add("浙");
        itemList.add("皖");
        itemList.add("闽");
        itemList.add("赣");
        itemList.add("鲁");
        itemList.add("豫");
        itemList.add("鄂");
        itemList.add("湘");
        itemList.add("桂");
        itemList.add("琼");
        itemList.add("渝");
        itemList.add("川");
        itemList.add("贵");
        itemList.add("云");
        itemList.add("藏");
        itemList.add("陕");
        itemList.add("甘");
        itemList.add("青");
        itemList.add("宁");
        itemList.add("新");
        itemList.add("港");
        itemList.add("澳");
        itemList.add("台");
    }


    /**
     * 时间选择对话框
     *
     * @param context
     */
    public static void showDialogTime(Context context, final TextView cityTv) {
        CommonAdapter commonAdapter = null;
        final List<String> itemList = new ArrayList<String>();
        initTime(itemList);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(400,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        View dialogView = LayoutInflater.from(context).inflate(R.layout.view_dialog_time, null);

        ListView dialog_choice_lv = (ListView) dialogView.findViewById(R.id.dialog_choice_lv);
        TextView tv = (TextView) dialogView.findViewById(R.id.platetv);
        dialogView.findViewById(R.id.close_prompt).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogUtil.getInstance().dismiss();
            }
        });

        dialog_choice_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DialogUtil.getInstance().dismiss();
                cityTv.setText(itemList.get(position));
            }
        });

        if (null == commonAdapter) {
            commonAdapter = new CommonAdapter<String>(context,
                    itemList, R.layout.item_dialog_time, false) {
                @SuppressWarnings("incomplete-switch")
                @Override
                public void convert(ViewHolder viewHolder, String item) {
                    // TODO Auto-generated method stub
                    viewHolder.setText(R.id.platetv, item);
                }
            };
            dialog_choice_lv.setAdapter(commonAdapter);
        } else {
            commonAdapter.notifyDataSetChanged();
        }
        DialogUtil.getInstance().showDialog(context, dialogView, true, params);
    }

    public static void initTime(List<String> itemList) {
        itemList.add("不启用");
        itemList.add("30秒");
        itemList.add("60秒");
        itemList.add("90秒");
        itemList.add("120秒");
        itemList.add("240秒");
    }

    public interface OnDialogItemClickListener {

        void onCancle();

        void onSure();
    }

}
